package com.yijiaganxi.member.web.coupon;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yijiaganxi.common.entity.coupon.YjgxCoupon;
import org.yijiaganxi.common.res.R;
import org.yijiaganxi.common.vo.web.coupon.YjgxCouponWebVo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import org.yijiaganxi.common.GlobalConstants;
import com.yijiaganxi.member.service.coupon.YjgxCouponService;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotBlank;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 优惠券管理 前端控制器
 * </p>
 *
 * @author zhangguowei123
 * @since 2020-08-30
 */
@Validated
@RestController
@RequestMapping("/member/yjgxCoupon/"+GlobalConstants.WEB_FLAG)
public class YjgxCouponController {

	@Autowired
	public YjgxCouponService couponService;
	
	
	/**
	 * 查询优惠券列表
	 * @return
	 */
	@GetMapping("selectAllCouponList")
	public R selectAllCouponList() {
		List<YjgxCoupon> selectList = couponService.selectList(new EntityWrapper<YjgxCoupon>());
		if(CollectionUtils.isEmpty(selectList))
			return R.error("没有优惠券信息");
		
		List<YjgxCouponWebVo> list=Lists.newArrayList();
		YjgxCouponWebVo webVo;
		for(YjgxCoupon couponItem: selectList) {
			webVo=new YjgxCouponWebVo();
			BeanUtils.copyProperties(couponItem, webVo);
			webVo.setTimeRangeStr(couponItem.getStartDate()+" 至 "+couponItem.getEndDate());
			list.add(webVo);
		}
		return R.opertionListResponse(list);
	}

	
	/**
	 * 删除优惠券
	 * @param id
	 * @return
	 */
	@PostMapping("deleteCouponById")
	public R deleteCouponById(@NotBlank(message="id不能为空")String id) {
		return R.insertOrUpdateResponse(couponService.deleteById(id));
	}
	
	/**
	 * 添加优惠券
	 * @param coupon
	 * @return
	 */
	@PostMapping("addOrUpdateCoupon")
	public R addOrUpdateCoupon(@Validated YjgxCoupon coupon) {
		if(coupon.getId()!=null)
			return R.insertOrUpdateResponse(couponService.updateById(coupon));
		return R.insertOrUpdateResponse(couponService.insert(coupon));
	}
	
	/**
	 * 查询优惠券详情
	 * @param id
	 * @return
	 */
	@GetMapping("selectCouponDetailById")
	public R selectCouponDetailById(@NotBlank(message="id必填")String id) {
		return R.opertionPojoResponse(couponService.selectById(id));
	}
}

